package com.demo.main.servlet;

import com.demo.main.entity.Notice;
import com.demo.main.service.NoticeService;
import com.demo.main.utils.ServletUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.demo.main.utils.ServletUtil.PERMISSION_PATH_ADMIN;


@WebServlet({PERMISSION_PATH_ADMIN + "/notice_condition",
        PERMISSION_PATH_ADMIN + "/notice_edit",
        PERMISSION_PATH_ADMIN + "/notice_add",
        PERMISSION_PATH_ADMIN + "/notice_delete"})
public class NoticeServlet extends HttpServlet {
    private final NoticeService noticeService = new NoticeService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletUtil servletUtil = new ServletUtil(request, response);
        switch (servletUtil.getServletPath()) {
            case PERMISSION_PATH_ADMIN + "/notice_condition":
                request.setAttribute("list", noticeService.findByCondition(servletUtil.convertRequestParameter(Notice.class)));
                servletUtil.forward(PERMISSION_PATH_ADMIN + "/view/notice_management");
                break;
            case PERMISSION_PATH_ADMIN + "/notice_edit":
                noticeService.updateById(servletUtil.convertRequestParameter(Notice.class));
                servletUtil.forward(PERMISSION_PATH_ADMIN + "/view/notice_management");
                break;
            case PERMISSION_PATH_ADMIN + "/notice_add":
                noticeService.addOne(servletUtil.convertRequestParameter(Notice.class));
                servletUtil.forward(PERMISSION_PATH_ADMIN + "/view/notice_management");
                break;
            case PERMISSION_PATH_ADMIN + "/notice_delete":
                // ajax
                noticeService.deleteById(servletUtil.getParameterIntegerId());
                servletUtil.jsonDeleteSuccess();
                break;
        }
    }
}
